<%@page import="multiface.valentinorusso.jpa.util.PersistenceManager"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page import="com.mysql.jdbc.Util"%>
<%@page import="multiface.web.util.categories.AlberoCategorie"%>
<%@page import="multiface.valentinorusso.jpa.Categoria"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%
    String action = request.getParameter("action");
    String pag = request.getParameter("page");
    String cid = request.getParameter("cid");

    EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();


    String title = "Aggiungi un Lavoro";

    List<Categoria> list = em.createNativeQuery("SELECT * FROM categoria", Categoria.class).getResultList();



%>
<script>
    $(document).ready(function(){
        $("#formBox").validate();
        nicEditors.allTextAreas({buttonList : ['bold','italic','underline','ul','ol']});
       
        
    });
    
    function select(cid){
        $('#categoria').val(cid);
        $('.cats').css('font-weight','normal');
        $('#cat_'+cid).css('font-weight','bold');
    }
    
    function save(){
        if($("#formBox").valid()){
            $.post("ActionServlet", 
            { 
                nome:$("#nome").val(),
                cid:$("#cid").val(),
                descrizione: $("#descrizione").prev().children().html(),
                adm:'<%=session.getId()%>',
                action:'<%=action%>'
            },
            function(data){ 
                location.href= "index.jsp?adm=<%=session.getId()%>&page=<%=pag%>&sez=lavori";
            });
            
        }
    }
</script>

<style>
    .collegato{
        border-bottom: 1px #ccc solid;
        padding: 6px;
        color: #666p;
    }
    tr{
        border-bottom: 1px #eee dotted;
    }
</style>


<div id="content" style="margin-top: 10px;width: 620px;float: left;text-align: left;">
    <div class="title">
        <%=title%>
    </div>
    <form class="cmxform" id="formBox" method="post" action="">
        <table class="table-form" >
            <tr>
                <td style="width: 220px;">Nome *</td>
                <td style="width: 480px;"><input type="text" id="nome" name="nome" size="50" class="required" minlength="4" /></td>
            </tr>
            <tr>
                <td style="width: 220px;">Lavorazione *</td>
                <td style="width: 480px;">
                    <select id="cid" name="cid">
                        <%for (Categoria c : list) {
                                String select = "";
                                if (cid != null) {
                                    if (c.getIdcategoria() == Integer.parseInt(cid)) {
                                        select = "selected";
                                    }
                                }
                        %>

                        <option <%=select%> value="<%=c.getIdcategoria()%>"><%=c.getNome()%></option>
                        <%}%>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Descrizione</td>
                <td><textarea id="descrizione" name="descrizione" cols="50" rows="15" minlength="10" ></textarea></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <div><i>*Campi obbligatori</i></div>
                </td>
            </tr>
        </table>


        <div class="save-form"><a href="javascript:save()">SALVA</a></div> 


    </form>

</div>

<%
    em.close();
%>